$NG.AllReady(function (page, { useUpdateRow, useValuesChange, useBeforeClick, useDataIndexChange }) {
    const mstform = $NG.getCmpApi("p_form_unforeseen_fees_chg");

    {//初始化逻辑BEGIN
        if (mstform) {
            console.log(mstform.name + ' is ready');
        } else {
            console.error('mstform is not available');
        };
    }//初始化逻辑END

    if (page.oType == "add") {
        var pc = mstform.getItem("phid_pc");
        console.log("123123");
        if (pc.getValue() > "0") {
            getsbqd()
        }

    }

    //项目名称变化时自动带入产品线、事业部等
    useValuesChange(({ args }) => {
        console.log(args[0]);
        const phid_pc = args[0].phid_pc.value;
        if (phid_pc) {
            console.log(phid_pc);
            getsbqd()
        }

    }, "p_form_unforeseen_fees_chg.phid_pc");



    useValuesChange(({ args }) => {
        var u_htxe = mstform.getItem('u_htxe').getValue();
        var u_xmzcb = mstform.getItem('u_xmzcb').getValue();
        var u_fwcb = mstform.getItem('u_fwcb').getValue();
        var u_unforeseeable_ori_ze = mstform.getItem('u_unforeseeable_ori_ze').getValue();
        var u_unforeseeable_chg_amt = mstform.getItem('u_unforeseeable_chg_amt').getValue();

        var mll= (u_htxe - (u_xmzcb-u_fwcb+u_unforeseeable_chg_amt)) / u_htxe;
        mstform.getItem('u_xmzcb').setValue(u_unforeseeable_chg_amt+window._catch_xmzcb)
        mstform.getItem('u_mll_chg').setValue(mll)

        mstform.getItem('u_unforeseeable_chg_ze').setValue(u_unforeseeable_ori_ze+u_unforeseeable_chg_amt)

    }, "p_form_unforeseen_fees_chg.u_unforeseeable_chg_amt");



    useBeforeClick(async function ({ args }) {

        var fb = mstform.getItem('u_yfp_unforeseeable_fb').getValue();
        var zd = mstform.getItem('u_yfp_unforeseeable_zd').getValue();
        var fyze = mstform.getItem('u_unforeseeable_chg_ze').getValue();

        if ((fb + zd) > fyze) {
            $NG.alert("变更后不可预见费用总额小于已分配费用总额，请检查！")
            return false
        }

        else {
            return true
        }


    }, "save");


    function getsbqd() {
        var pcname = mstform.getItem("phid_pc").getRawValue().label;
        var bill_dt = mstform.getItem("bill_dt").getValue();

        $NG.execServer("GetSbUnforeseeable_Info", { pc: mstform.getItem('phid_pc').getValue() }, function (res) {
            console.log(res);
            if (res.count == "1") {
                const data = JSON.parse(res.data);
                const u_unforeseeable_ze = data[0].extendObjects.u_unforeseeable_ze;
                const u_unforeseeable_zdys = data[0].extendObjects.u_unforeseeable_zdys;
                const u_unforeseeable_fbfs = data[0].extendObjects.u_unforeseeable_fbfs;
                const u_unforeseeable_zdfs = data[0].extendObjects.u_unforeseeable_zdfs;
                const u_unforeseeable_fbys = data[0].extendObjects.u_unforeseeable_fbys;
                const u_unforeseeable_zefs = data[0].extendObjects.u_unforeseeable_zefs;
                const htxe = data[0].extendObjects.htxe;
                const xmzcb = data[0].extendObjects.xmzcb;
                const fwcb = data[0].extendObjects.fwcb;
                const qdmll = data[0].extendObjects.qdmll;
                window._catch_xmzcb = xmzcb
                $NG.updateState((updater) => {
                    updater.data.p_form_unforeseen_fees_chg.setProps({
                        u_unforeseeable_ori_ze: u_unforeseeable_ze,
                        u_unforeseeable_zefs: u_unforeseeable_zefs,
                        u_unforeseeable_yfszd: u_unforeseeable_zdfs,
                        u_unforeseeable_yfsfb: u_unforeseeable_fbfs,
                        u_yfp_unforeseeable_zd: u_unforeseeable_zdys,
                        u_yfp_unforeseeable_fb: u_unforeseeable_fbys,
                        u_htxe: htxe,
                        u_xmzcb: xmzcb,
                        u_fwcb: fwcb,
                        u_mll_ori: qdmll,
                        bill_name: bill_dt + pcname + "不可预见费用变更",
                    });
                });

            }
        });

    }

});



